#!/bin/bash
#Author: weike
#MyQQID: 2362317758
export TOPDIR=$PWD

export MODPKG_ROOT=${TOPDIR}/pkg
export MODSRC_ROOT=${TOPDIR}/src

export MODNAME=rc.local

export MODPKG_NAME=${MODNAME}.tar.gz
export MODSRC_NAME=${MODNAME}

export MODPKG_PATH=${MODPKG_ROOT}/${MODPKG_NAME}
export MODSRC_PATH=${MODSRC_ROOT}/${MODSRC_NAME}
export MODOUT_PATH=$TOPDIR/output

export NET_CFGLIST_NAME=net_conf.lst
export NET_CFGHEAD_NAME=net_conf.h
export NET_CFGROOT_PATH=${MODSRC_PATH}/config

export NET_CFGLIST_PATH=${NET_CFGROOT_PATH}/${NET_CFGLIST_NAME}
export NET_CFGHEAD_PATH=${MODSRC_PATH}/${NET_CFGHEAD_NAME}

#-----------------------------------------------------------------------
perro()
{
    echo -e "\033[47;41mERROR: $*\033[0m"
}

pwarn()
{
    echo -e "\033[47;43mWARN: $*\033[0m"
}

pinfo()
{
    echo -e "\033[47;44mINFO: $*\033[0m"
}

pdone()
{
    echo -e "\033[47;42mINFO: $*\033[0m"
}

#-----------------------------------------------------------------------

append_head()
{
	echo -e "#ifndef _NET_CONF_H"    >  ${NET_CFGHEAD_PATH}
	echo -e "#define _NET_CONF_H "   >> ${NET_CFGHEAD_PATH}
	echo -e "#include <ual_types.h>"  >> ${NET_CFGHEAD_PATH}
	echo -e "ual_net_node ual_net_node_list[] = {" >> ${NET_CFGHEAD_PATH}

}

append_tail()
{
	echo  "};"     >> ${NET_CFGHEAD_PATH}
	echo  "#endif" >> ${NET_CFGHEAD_PATH}
}

append_node_eth()
{
	echo  "    {"                          >> ${NET_CFGHEAD_PATH}
	echo  "        .type   = eth,"          >> ${NET_CFGHEAD_PATH}
	echo  "        .name   = \"${1}\","     >> ${NET_CFGHEAD_PATH}
	echo  "        .proto  = \"${2}\","     >> ${NET_CFGHEAD_PATH}
	echo  "        .ipaddr = \"${3}\","     >> ${NET_CFGHEAD_PATH}
	echo  "        .netmask= \"${4}\","     >> ${NET_CFGHEAD_PATH}
	echo  "        .gateway= \"${5}\","     >> ${NET_CFGHEAD_PATH}
	echo  "    },"                         >> ${NET_CFGHEAD_PATH}
}

apend_node_dns()
{
	echo  "    {"                      >> ${NET_CFGHEAD_PATH}
	echo  "        .type  = dns,"       >> ${NET_CFGHEAD_PATH}
	echo  "        .name  = \"${1}\","  >> ${NET_CFGHEAD_PATH}
	echo  "        .ipaddr= \"${2}\","  >> ${NET_CFGHEAD_PATH}
	echo  "    },"                     >> ${NET_CFGHEAD_PATH}
}

set_ual_eth()
{
	tmpline=$1
	is_eth_type=$(echo $tmpline | grep -w "type=eth")
	if [ -n "${is_eth_type}" ]; then
		arryline=$(echo $tmpline | sed 's/,/ /g')
		for kvgrp in $arryline
		do
			key=${kvgrp%%=*}
			val=${kvgrp##*=}
			if [ -n "${key}" ] && [ -n "${val}" ]; then
				if [ "xname" = "x${key}" ]; then
					export eth_name=${val}
				elif [ "xproto" = "x${key}" ]; then
					export eth_proto=${val}
				elif [ "xipaddr" = "x${key}" ]; then
					export eth_ipaddr=${val}
				elif [ "xnetmask" = "x${key}" ]; then
					export eth_netmask=${val}
				elif [ "xgateway" = "x${key}" ]; then
					export eth_gateway=${val}
				fi
			fi
		done
		if [ -n "${eth_name}" ] && [ -n "${eth_proto}" ] && [ -n "${eth_ipaddr}" ] && [ -n "${eth_netmask}" ] && [ -n "${eth_gateway}" ]; then
			append_node_eth  "${eth_name}" "${eth_proto}" "${eth_ipaddr}" "${eth_netmask}" "${eth_gateway}"
		fi
		unset eth_name
		unset eth_proto
		unset eth_ipaddr
		unset eth_netmask
		unset eth_gateway
	fi
}

set_ual_dns()
{
	tmpline=$1
	is_dns_type=$(echo $tmpline | grep -w "type=dns")
	if [ -n "${is_dns_type}" ]; then
		arryline=$(echo $tmpline | sed 's/,/ /g')
		for kvgrp in $arryline
		do
			key=${kvgrp%%=*}
			val=${kvgrp##*=}
			if [ -n "${key}" ] && [ -n "${val}" ]; then
				if [ "xname" = "x${key}" ]; then
					export dns_name=${val}
				elif [ "xipaddr" = "x${key}" ]; then
					export dns_ipaddr=${val}
				fi
			fi
		done
		if [ -n "${dns_name}" ] && [ -n "${dns_ipaddr}" ]; then
			apend_node_dns ${dns_name} ${dns_ipaddr}
		fi
		unset dns_name
		unset dns_ipaddr
	fi
}

parse_network_conf()
{
	while read line
	do
		procline=$(echo $line | sed 's/ //g')
		validline=$(echo $procline | grep -v "^#")
		validline=$(echo $validline | grep  "^type=")
		if [ -z "$validline" ]; then
			continue;
		fi
		set_ual_eth   $procline
		set_ual_dns   $procline
	done < ${NET_CFGLIST_PATH}
}


if [ ! -e ${NET_CFGLIST_PATH} ]; then
	perro "NO NET_CFGLIST_PATH: ${NET_CFGLIST_PATH}"
	exit 1
fi
pinfo "Start create: $NET_CFGHEAD_PATH"
append_head
parse_network_conf
append_tail
pdone "Finish create: $NET_CFGHEAD_PATH"


